<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ include file="../common/taglibs.jsp"%>
<%@ page import="java.util.*"%>
<%@ page import="com.opensymphony.xwork2.ActionSupport"%>
<%@ page import="net.aiform.account.pojo.*"%>
<%@ page import="net.aiform.bean.Constant"%>
<%@ page import="net.aiform.template.pojo.*"%>
<%@ page import="ext.cjt.*"%>
<html>
<meta http-equiv="content-type" content="text/html;charset=UTF-8">
<script src="${ctx}/static/js/jquery.js" type="text/javascript"></script>
<script src="${ctx}/static/js/jquery.url.js" type="text/javascript"></script>
<script src="${ctx}/static/js/ui.core.js" type="text/javascript"></script>
<script src="${ctx}/static/js/ui.sortable.js" type="text/javascript"></script>
<script src="${ctx}/static/js/s.js" type="text/javascript"></script>
<head>
<style type="text/css">
.af-t-field-name {
	text-align: right;
	width: 90px;
}

.af-t-field {
	width: 200px;
}

.af-t-field-input {
	width: 100px;
}
</style>
<title>
<%
	ActionSupport as_index = new ActionSupport();
	out.print(as_index.getText("sys.title", new String[]{as_index
			.getText("sys.name")}));
%>
</title>
</head>
<script type="text/javascript">
	function onsubmit_field_form() {
		var fieldListParent=$("#field_list")[0];
		var fieldList=fieldListParent.children;
		for(var i=0;i<fieldList.length;i++){
			var fieldArea=fieldList[i];
			changeFieldName(fieldArea,i);
		}
		return true;
	}
	function changeFieldName(area,count) {
		//count需要保存到全局才行，each方法传不进去。
		window[area.id]=count;
		$('#'+area.id+' * ').each(function(){
			var fieldName=this.name;
			if(undefined==fieldName)return;
			if(fieldName.match(new RegExp(AiForm.params+'\\[(\\w)+\\]\.(\\w)+'))){
				var groupId=fieldName.substring(fieldName.indexOf("[")+1,fieldName.indexOf("]"));
				var newName=fieldName.replace(groupId,window[groupId]);
				this.name=newName;
			}
        });
	}
	function init_template() {
		var code = jQuery.url.attr("file");
		$("#template.code").val(code);
		fetch_template(code);
	}
	function customize_template(message) {
		if ("403" == message.code) {
			$("#recordarea").html("<h3 style='color:red'>没有权限</h3>");
			return;
		} else {
			var template = message.data[0];
			if (null == template)
				return;
			init_template_exec(template);
		}
	}
	function init_template_exec(template) {
			$("#template.id").val(template.id);
	}
	function fetch_template(code) {
		$.ajax( {
			url : "${ctx}/t/list/" + code,
			type : "post",
			dataType : "json",
			timeout : 5000,
			error : function() {
				alert("Error loading XML document");
			},
			success : function(root) {
				var json = root.json;
				fetch_template_callback(json);
			}
		});
	}
	function fetch_template_callback(message) {
		customize_template(message);
	}
	function add_field() {
		var id = AiForm.idPrefix + AiForm.random(12);
		var obj = get_new_field(id);
		$('#field_list').append(obj);
		$('#field_list').sortable();
	}
	function get_new_field(id) {
		var frame = $('<div id="' + id + '" style="margin-bottom:5px;border:1px dashed gray;padding:2 2 5 2;background-color:white;cursor:move"><div align="right">[<a href="#" class="control-link" onclick="this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode)">X</a>]</div></div>');
		var category = $('<div style="float:left;" class="af-t-field"><span class="af-t-field-name">类　　型：</span><span><select name="'+AiForm.params+'['+id+'].category"><option value="string">字符串</option><option value="int">整数</option><option value="double">小数</option><option value="date">日期</option><option value="time">时间</option><option value="datetime">日期时间</option><option value="singleselect">单选</option><option value="multiselect">多选</option><option value="rater">评分器</option><option value="formula">公式</option><option value="file">文件</option></select></span></div>');
		frame.append(category);
		var name = $('<div style="float:left;" class="af-t-field"><span class="af-t-field-name">名　　称：</span><span><input name="'+AiForm.params+'['+id+'].name" class="af-t-field-input"></input></span></div>');
		frame.append(name);
		var code = $('<div style="float:left;" class="af-t-field"><span class="af-t-field-name">编　　码：</span><span><input name="'+AiForm.params+'['+id+'].code"class="af-t-field-input"></input></span></div>');
		frame.append(code);
		var length = $('<div style="float:left;" class="af-t-field"><span class="af-t-field-name">长　　度：</span><span><input name="'+AiForm.params+'['+id+'].length"class="af-t-field-input"></input></span></div>');
		frame.append(length);
		var hidden = $('<div><span>是否隐藏：</span><span><input type="checkbox" name="'+AiForm.params+'['+id+'].hidden"></input></span></div><br>');
		frame.append(hidden);
		var subArea=$('<div></div>');
		frame.append(subArea);
		var defaultValue = $('<div style="float:left;" class="af-t-field"><span class="af-t-field-name">默认值　：</span><span><input name="'+AiForm.params+'['+id+'].defaultValue"class="af-t-field-input"></input></span></div>');
		subArea.append(defaultValue);
		var exampleValue = $('<div style="float:left;" class="af-t-field"><span class="af-t-field-name">样例值　：</span><span><input name="'+AiForm.params+'['+id+'].exampleValue"class="af-t-field-input"></input></span></div>');
		subArea.append(exampleValue);
		var restrain = $('<div style="float:left;" class="af-t-field"><span class="af-t-field-name">约　　束：</span><span><input name="'+AiForm.params+'['+id+'].restrain"class="af-t-field-input"></input></span></div>');
		subArea.append(restrain);
		var restrainRemark = $('<div><span class="af-t-field-name">约束备注：</span><span><input name="'+AiForm.params+'['+id+'].restrainRemark"class="af-t-field-input"></input></span></div><br>');
		subArea.append(restrainRemark);
		var remark = $('<div><span class="af-t-field-name">说　　明：</span><span><textarea name="'+AiForm.params+'['+id+'].remark"></textarea></span></div>');
		subArea.append(remark);
		return frame;
	}
</script>
<body onload="init_template();">
<%@ include file="../common/userInfo.jsp"%>
<hr>
<div>
<form name="field_form" onsubmit="return onsubmit_field_form();"
	action="${ctx}/t/save" method="post"><input type="hidden"
	name="template.id" id="template.id">
<table>
	<tr>
		<td>名称:</td>
		<td><input name="template.name"></input></td>
		<td align="right">编码:</td>
		<td><input name="template.code" id="template.code"></input></td>
		<td>表单类型:</td>
		<td><select name="template.category">
			<option value="m1r1">一人一次</option>
			<option selected="selected" value="m1rn">一人多次</option>
			<option value="mnr1">多人一次</option>
			<option value="mnrn">多人多次</option>
		</select></td>
	</tr>
	<tr> 
		<td>填报开始时间:</td>
		<td><input name="template.recordStart"
			value="<%=DateTools.getDatetimeString(new Date())%>"></input></td>
		<td>填报结束期限:</td>
		<td><input name="template.recordEnd"
			value="<%=DateTools.getDatetimeString(DateTools.add(new Date(),
							DateTools.MONTH, 1))%>"></input></td>
		<td>结果保留期限:</td>
		<td><input name="template.statisticsEnd"
			value="<%=DateTools.getDatetimeString(DateTools.add(new Date(),
							DateTools.DAY_OF_YEAR, 50))%>"></input></td>
	</tr>
	<tr>
		<td><input type="checkbox" name="random" id="random" /><label
			for="random">是否随机排序？</label></td>
		<td><input type="checkbox" name="recordUser" id="recordUser" /><label
			for="recordUser">是否记录填报者用户名？</label></td>
		<td><input type="checkbox" name="recordIp" id="recordIp" /><label
			for="recordIp">是否记录填报者IP？</label></td>
		<td><input type="checkbox" name="recordTime" id="recordTime" /><label
			for="recordTime">是否记录填报时间？</label></td>
		<td></td>
	</tr>
	<tr>
		<td>填报权限控制:</td>
		<td><select name="template_recordAC">
			<option value="anonymous">公开</option>
			<option value="password">密码</option>
			<option value="designateduser">指定用户</option>
			<option value="designatedgroup">指定群组</option>
			<option value="designatedip">指定IP</option>
			<option value="authenticatedcode">认证码</option>
		</select></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
	</tr>
	<tr>
		<td>结果查看权限控制:</td>
		<td><select name="template_resultAC">
			<option value="creator">创建者</option>
			<option value="recorder">填报者</option>
			<option value="anonymous">公开</option>
			<option value="password">密码</option>
			<option value="designateduser">指定用户</option>
			<option value="designatedgroup">指定群组</option>
			<option value="designatedip">指定IP</option>
			<option value="authenticatedcode">认证码</option>
		</select></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
	</tr>
	<tr>
		<td>统计查询权限控制:</td>
		<td><select name="template_statisticsAC">
			<option value="creator">创建者</option>
			<option value="recorder">填报者</option>
			<option value="anonymous">公开</option>
			<option value="password">密码</option>
			<option value="designateduser">指定用户</option>
			<option value="designatedgroup">指定群组</option>
			<option value="designatedip">指定IP</option>
			<option value="authenticatedcode">认证码</option>
		</select></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
	</tr>
	<tr>
		<td>填报指南:</td>
		<td><textarea name="template.header"></textarea></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
	</tr>
	<tr>
		<td>附属说明:</td>
		<td><textarea name="template.footer"></textarea></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
	</tr>
</table>


<br>
<button onclick="add_field();return false;">+</button>
<div id="field_list"></div>

<button type="submit">Submit</button>
</form>


</div>
<div id="testarea"></div>
</body>
</html>

